//
// Created by YxMin on 2024/7/6.
//
#include <procedure.h>
#include <UI.h>
#include <network.h>
#include <cstdio>
int foundRoomCount = 0;
RemoteRoomInfo RoomList[10];
char *RoomInfoString[10];

void AddRemoteRoom(char* RoomInfo, char* Ip) //由外部调用，添加发现的room记录，并显示在ui上
{
    for (int i = 0; i < foundRoomCount; ++i) {
        if (strcmp(Ip,RoomList[i].Ip) == 0) {
            return; //房间已存在
        }
    }
    RoomList[foundRoomCount].Ip = new char[16];
    strcpy(RoomList[foundRoomCount].Ip,Ip);
    RoomList[foundRoomCount].Info = &RoomInfo[1];
    RoomList[foundRoomCount].OnlinePlayerCount = RoomInfo[0] & 0x0f;
    printf("发现房间:(%s)%s 人数:%d/4\n",RoomList[foundRoomCount].Ip,RoomList[foundRoomCount].Info,RoomList[foundRoomCount].OnlinePlayerCount);
    RoomInfoString[foundRoomCount] = new char[64];
    sprintf(RoomInfoString[foundRoomCount],"[%s](%d/4)",/*RoomList[foundRoomCount].Info,*/RoomList[foundRoomCount].Ip,RoomList[foundRoomCount].OnlinePlayerCount);
    AddRoomToListUI(RoomInfoString[foundRoomCount]);
    foundRoomCount++;
}
void ClearRoomList()
{
    for (int i = 0; i < foundRoomCount; ++i) {
        delete(RoomList[i].Info-1);
        delete(RoomList[i].Ip);
        delete RoomInfoString[i];
        RoomList[i].OnlinePlayerCount = 0;
    }
    foundRoomCount = 0;
}
void StopFindRoom()
{
    StopReceiveUdp();
    ClearRoomList();
}